package com.kainitram.drivebook;

import android.os.Bundle;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.kainitram.drivebook.data.DatabaseHelper;
import com.kainitram.drivebook.fragments.HomeFragment;
import com.kainitram.library.sm.BaseSlidingFragmentActivity;
import com.kainitram.library.sm.SlidingContentFragment;

public class DriveBookMainActivity extends BaseSlidingFragmentActivity {

	private DatabaseHelper databaseHelper = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setBehindContentView(R.layout.menu_frame);
		getFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, new DriveBookPreferenceFragment())
				.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.drive_book_main, menu);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			Toast toast = Toast.makeText(this, "Setting", Toast.LENGTH_SHORT);
			toast.show();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public String getApplicationTitle() {
		String ret = getResources().getString(R.string.app_name);

		return ret;
	}

	/**
	 * Get database helpers
	 * 
	 * @return database helper
	 */
	public DatabaseHelper getDatabaseHelper() {
		if (databaseHelper == null) {
			databaseHelper = OpenHelperManager.getHelper(this,
					DatabaseHelper.class);
		}
		return databaseHelper;
	}

	@Override
	protected int getContentFrameID() {
		return R.id.content_frame;
	}

	@Override
	protected SlidingContentFragment getDefaultContentFragment() {
		return new HomeFragment();
	}

	@Override
	protected int getSlidingActivityViewID() {
		return R.layout.activity_drive_book_main;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (databaseHelper != null) {
			OpenHelperManager.releaseHelper();
			databaseHelper = null;
		}
	}

}
